package com.guodu.dao.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.guodu.base.BaseDao;
import com.guodu.dao.MaterialStoreDao;
import com.guodu.entity.MaterialStore;
import com.guodu.entity.MaterialstoreStatus;
import com.guodu.entity.Units;
import com.guodu.vo.MaterialStoreQueryVo;

@Repository("materialStoreDao")
public class MaterialStoreDaoImpl extends BaseDao implements MaterialStoreDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialStore> findByVo(MaterialStoreQueryVo materialStoreQueryVo,Units units,
			int curPage, int pageSize) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from MaterialStore m where 1=1");
		if(materialStoreQueryVo!=null){
			if(materialStoreQueryVo.getDrawingnum()!=null&&materialStoreQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialStoreQueryVo.getDrawingnum()+"%'");
			}
			if(materialStoreQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId = m.unitsId and u.funitsId="+materialStoreQueryVo.getFunitsId()+" )");
			}
			if(materialStoreQueryVo.getUnitsId()!=null){
				hql.append(" and m.unitsId= "+materialStoreQueryVo.getUnitsId());
			}
			if(materialStoreQueryVo.getMaterialName()!=null&&materialStoreQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and m.materialName like '%"+materialStoreQueryVo.getMaterialName()+"%'");
			}
			if(materialStoreQueryVo.getSpecifications()!=null&&materialStoreQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and m.specifications like '%"+materialStoreQueryVo.getSpecifications()+"%'");
			}
			if(materialStoreQueryVo.getPlace()!=null&&materialStoreQueryVo.getPlace().trim().length()>0){
				hql.append(" and m.place like '%"+materialStoreQueryVo.getPlace()+"%'");
			}
			if(materialStoreQueryVo.getStoreManufacturer()!=null&&materialStoreQueryVo.getStoreManufacturer().trim().length()>0){
				hql.append(" and m.storeManufacturer like '%"+materialStoreQueryVo.getStoreManufacturer()+"%'");
			}
			if(materialStoreQueryVo.getTexture()!=null&&materialStoreQueryVo.getTexture().trim().length()>0){
				hql.append(" and m.texture like '%"+materialStoreQueryVo.getTexture()+"%'");
			}
			if(materialStoreQueryVo.getCreateBeginTime()!=null&&materialStoreQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and m.createTime >= TO_DATE('"+materialStoreQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialStoreQueryVo.getCreateEndTime()!=null&&materialStoreQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and m.createTime <= TO_DATE('"+materialStoreQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialStoreQueryVo.getStatus()!=null&&materialStoreQueryVo.getStatus().trim().length()>0&&!materialStoreQueryVo.getStatus().equals("5")){
				if(materialStoreQueryVo.getStatus().equals("10")){
					hql.append("and m.status=0 and exists(select 1 from UserInfo u where m.createPeople=u.userId and exists(select 1 from Units n where n.unitsId = u.unitsId and n.unitsLevel=1))");
				}else if(materialStoreQueryVo.getStatus().equals("20")){
					hql.append("and m.status=0 and exists(select 1 from UserInfo u where m.createPeople=u.userId and exists(select 1 from Units n where n.unitsId = u.unitsId and n.unitsLevel=2))");
				}else{
					hql.append(" and m.status ="+materialStoreQueryVo.getStatus());
				}
			}
		}
		if (units == null) {
			hql.append(" and ((m.status=0 and exists(select 1 from UserInfo u where m.createPeople=u.userId and exists(select 1 from Units n where n.unitsId = u.unitsId and n.unitsLevel=1))) or status=1 or status=3 or status=4)");
		} 
		hql.append(" and store = '" + materialStoreQueryVo.getStoreId() + "'");
		hql.append(" order by storeId desc");
		return super.findPages(hql.toString(), curPage, pageSize);
	}

	@Override
	public int findSizeByVo(
			MaterialStoreQueryVo materialStoreQueryVo,Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("select count(*) from MaterialStore m where 1=1");
		if(materialStoreQueryVo!=null){
			if(materialStoreQueryVo.getDrawingnum()!=null&&materialStoreQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialStoreQueryVo.getDrawingnum()+"%'");
			}
			if(materialStoreQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId = m.unitsId and u.funitsId="+materialStoreQueryVo.getFunitsId()+" )");
			}
			if(materialStoreQueryVo.getUnitsId()!=null){
				hql.append(" and m.unitsId= "+materialStoreQueryVo.getUnitsId());
			}
			if(materialStoreQueryVo.getMaterialName()!=null&&materialStoreQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and m.materialName like '%"+materialStoreQueryVo.getMaterialName()+"%'");
			}
			if(materialStoreQueryVo.getSpecifications()!=null&&materialStoreQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and m.specifications like '%"+materialStoreQueryVo.getSpecifications()+"%'");
			}
			if(materialStoreQueryVo.getPlace()!=null&&materialStoreQueryVo.getPlace().trim().length()>0){
				hql.append(" and m.place like '%"+materialStoreQueryVo.getPlace()+"%'");
			}
			if(materialStoreQueryVo.getStoreManufacturer()!=null&&materialStoreQueryVo.getStoreManufacturer().trim().length()>0){
				hql.append(" and m.storeManufacturer like '%"+materialStoreQueryVo.getStoreManufacturer()+"%'");
			}
			if(materialStoreQueryVo.getTexture()!=null&&materialStoreQueryVo.getTexture().trim().length()>0){
				hql.append(" and m.texture like '%"+materialStoreQueryVo.getTexture()+"%'");
			}
			if(materialStoreQueryVo.getCreateBeginTime()!=null&&materialStoreQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and m.createTime >= TO_DATE('"+materialStoreQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialStoreQueryVo.getCreateEndTime()!=null&&materialStoreQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and m.createTime <= TO_DATE('"+materialStoreQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialStoreQueryVo.getStatus()!=null&&materialStoreQueryVo.getStatus().trim().length()>0&&!materialStoreQueryVo.getStatus().equals("5")){
				if(materialStoreQueryVo.getStatus().equals("10")){
					hql.append("and m.status=0 and exists(select 1 from UserInfo u where m.createPeople=u.userId and exists(select 1 from Units n where n.unitsId = u.unitsId and n.unitsLevel=1))");
				}else if(materialStoreQueryVo.getStatus().equals("20")){
					hql.append("and m.status=0 and exists(select 1 from UserInfo u where m.createPeople=u.userId and exists(select 1 from Units n where n.unitsId = u.unitsId and n.unitsLevel=2))");
				}else{
					hql.append(" and m.status ="+materialStoreQueryVo.getStatus());
				}
			}
		}
		if (units == null) {
			hql.append(" and ((m.status=0 and exists(select 1 from UserInfo u where m.createPeople=u.userId and exists(select 1 from Units n where n.unitsId = u.unitsId and n.unitsLevel=1))) or status=1 or status=3 or status=4)");
		} 
		hql.append(" and store = '" + materialStoreQueryVo.getStoreId() + "'");
		return super.findInt(hql.toString());
	}

	@Override
	public MaterialStore findById(String id) {
		// TODO Auto-generated method stub
		return (MaterialStore) super.findById(MaterialStore.class, id);
	}

	@Override
	public void del(String id) {
		// TODO Auto-generated method stub
		super.delete(MaterialStore.class, id);
	}

	@Override
	public void delByIds(String[] ids) {
		// TODO Auto-generx`ated method stub
		super.deleteAllById(MaterialStore.class, ids);
	}

	@Override
	public void save(MaterialStore entity) {
		// TODO Auto-generated method stub
		entity.setStoreId("1");
		entity.setCreateTime(Calendar.getInstance().getTime());
		super.save(entity);
	}

	@Override
	public void examine(String ids, Long status) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update MaterialStore set status =");
		hql.append(status);
		hql.append(" where storeId in (");
		hql.append(ids);
		hql.append(")");
		super.executeUpdate(hql.toString());
	}

	@Override
	public void update(MaterialStore entity) {
		// TODO Auto-generated method stub
		super.update(entity);
	}

	@Override
	public void changeStatus(List<MaterialstoreStatus> entities) {
		// TODO Auto-generated method stub
		super.saveAll(entities);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialstoreStatus> findStatus(String id) {
		// TODO Auto-generated method stub
		return super.findByProperty(MaterialstoreStatus.class, "materialId", id);
	}

	@Override
	public void delByStoreId(String storeId) {
		// TODO Auto-generated method stub
		MaterialStore materialStore = new MaterialStore();
		materialStore.setStoreId(storeId);
		super.delete(materialStore);
	}


}
